package cj.web.pay.controllers;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;

import org.springframework.beans.factory.annotation.Autowired;

import cj.web.constants.MemuEnum;
import cj.web.constants.PayWayEnum;
import cj.web.constants.SCOrderStatusEnum;
import cj.web.pay.alipay.config.AlipayConfig;
import cj.web.pay.alipay.interfaces.PayNotify;
import cj.web.pay.alipay.util.AlipayNotify;
import cj.web.pay.alipay.util.FmtRequestParams;
import cj.web.pay.alipay.util.PriceFormat;
import cj.web.sc.dao.OrderDAO;

@Path("SCNotify")
public class SCOrderNotifyController extends PayNotify{
	@Autowired
	private OrderDAO orderDao;
	
	@Post("ali")
	public void alipayNotify(@Param("out_trade_no")String out_trade_no, @Param("trade_no")String trade_no, 
			@Param("trade_status")String trade_status, HttpServletRequest request,HttpServletResponse response) throws Exception{
		notify(out_trade_no, trade_no, trade_status, request, response);
	}
	
	/**
	 * 支付宝支付成功页面
	 * @param model
	 * @param out_trade_no
	 * @param trade_status
	 * @param request
	 * @return
	 */
	@Get("aliScs")
	public String aliSuccess(Model model, @Param("out_trade_no")String out_trade_no,@Param("trade_status")String trade_status,HttpServletRequest request){
		Map<String, String> params = FmtRequestParams.getRequestParams(request);
		model.add("currentTopMemu", MemuEnum.memuccsd);
		if(AlipayNotify.verify(params)){//验证成功
			if(trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")){
				List<Map> orders = orderDao.getOrderByNo(out_trade_no);
				if( orders != null && orders.size() > 0 ){
					model.add("obj", orders.get(0));
					return "/views/mallPC/paySuccess.vm";
				}
				else{
					return "/views/mallPC/payFail.vm";
				}
			}
			else{
				return "/views/mallPC/payFail.vm";
			}
		}
		else{//验证失败
			return "/views/mallPC/payFail.vm";
		}
	}
	
	@Override
	public void tradeFinished(String out_trade_no, String trade_no){
		List<Map> orders = orderDao.getOrderByNo(out_trade_no);
		if( orders != null && orders.size() > 0 ){
			Map order = orders.get(0);
			int status = Integer.parseInt(order.get("status").toString());
			Object wxTradeNo = order.get("wxTradeNo");
			//订单状态为待付款并且支付单号为空的时候才改状态
			if( status == SCOrderStatusEnum.prepay.getValue() && ( wxTradeNo == null || "".equals(wxTradeNo.toString()) ) ){
				orderDao.updateStatusByNO(SCOrderStatusEnum.paid.getValue(),out_trade_no, trade_no, PayWayEnum.ZFB.getValue()); //修改订单状态为已付款 更新支付订单号 支付方式
			}
		}
	}
	
	@Override
	public void tradeSuccess(String out_trade_no, String trade_no){
		tradeFinished(out_trade_no, trade_no);
	}
	
	@Override
	public boolean isNotifyInfoEqual(String outTradeNo, String notifyTotalFee, String notifySellerId){
		List<Map> orders = orderDao.getOrderByNo(outTradeNo);
		if( orders != null && orders.size() > 0 ){
			Map order = orders.get(0);
			BigDecimal bdPrice = new BigDecimal(order.get("discntPrice").toString());
			String price = PriceFormat.formatPrice(bdPrice.doubleValue());
			if( notifyTotalFee != null && notifySellerId != null &&
					notifyTotalFee.equals(price) && notifySellerId.equals(AlipayConfig.seller_id) ){
				return true;
			}
		}
		return false;
	}
}